<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="pyjm" class="w-1/4">
          <a-input v-model:value="formState.pyjm" placeholder="收费编号/收费标准/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div>
        <BaseButton type="primary" class="mb-3 mr-2" @click="onClickAdd()">新增</BaseButton>
        <BaseExport type="运维" name="收费标准" code="00001180" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" column-code="00001180" :data-source="tableData" :pagination="pagination" fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'id'">
            <a-popover placement="bottomLeft" :trigger="triggerMode" :auto-adjust-overflow="false" @open-change="(open) => open && getDataDetail(record.id)">
              <template #content>
                <div class="p-1.5">
                  <a-descriptions class="descriptions" size="middle" :column="4" bordered>
                    <a-descriptions-item v-for="item in dataSource" :key="item" :label="item?.mc + '系数'">{{ item.coefficient }}</a-descriptions-item>
                  </a-descriptions>
                </div>
              </template>
              <span ref="textRef" class="cursor-pointer text-primary hover:underline">{{ record.id }}</span>
            </a-popover>
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-tag :color="record.status === 0 ? 'green' : 'red'">{{ record.status === 0 ? '启用' : '停用' }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickAdd(record)">修改</a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onClickDetail(record.id)">明细</a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onClickSpecialProject(record.id)">特殊项目</a-button>
            <a-divider type="vertical" />
            <a-button v-if="record.status" type="link" size="small" @click="onClickEnable(record.id)">启用</a-button>
            <BaseButton v-else confirm-text="确认停用？" type="link" danger size="small" @click="onClickStop(record.id)">停用</BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { post } from '@/utils/request'
import BaseButton from '@/components/BaseButton'
import Edit from './Edit'

const router = useRouter()
const formState = ref({ pyjm: '' })
const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
const dataSource = ref([])

const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/chargeStandard/getChargeStandardList',
  immediate: true,
  params: formState.value
})
const onSuceess = () => {
  editOpen.value = false
  getData()
}
const onClickAdd = (row = {}) => {
  rowData.value = row
  editOpen.value = true
}

const onClickEnable = async (id) => {
  const res = await post('/operation/chargeStandard/doEnableChargeStandard', { id })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const onClickStop = async (id) => {
  const res = await post('/operation/chargeStandard/doDisableChargeStandard', { id })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}

const getDataDetail = async (id) => {
  const { code, data, msg } = await post('/operation/chargeStandard/getChargeStandardTypeList', { chargeStdId: id })
  if (code !== 0) return message.error(msg)
  dataSource.value = data
}

const onClickDetail = (id) => {
  router.push({
    path: '/setting/system/charging-standards-detail',
    query: {
      id
    }
  })
}
const onClickSpecialProject = (id) => {
  router.push({
    path: '/setting/system/charging-standards-special-projects',
    query: {
      id
    }
  })
}
</script>

<style lang="less" scoped>
.charge-number {
  color: var(--primary-color);
  cursor: pointer;
}
.descriptions {
  :deep(.ant-descriptions-row) {
    text-align: right;
    .ant-descriptions-item-label {
      text-align: right;
    }
  }
}
</style>
